﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmartSpace.Core;

namespace SmartSpace.Data
{
    public class FieldInstance
    {
        public string Value { get; private set; }
        public string Text { get;private set; }

        public PropertyMetadata Property { get; set; }
        public int OrderIndex { get; set; }

        public object Data { get; set; }

        public FieldInstance()
        {
        }

        public static void SetData(FieldInstance instance, string value)
        {
            IDataConvertor convertor = Convertors.Get(instance.Property.Type.Name);
            instance.Data = convertor.Convert(value);
            instance.Value = value;
            instance.Text = value;
        }

        public static void SetData(FieldInstance instance, string value, string text)
        {
            IDataConvertor convertor = Convertors.Get(instance.Property.Type.Name);
            instance.Data = convertor.Convert(value);
            instance.Value = value;
            instance.Text = text;
        }


        //public void SetValue(string value)
        //{
        //    if (string.IsNullOrEmpty(value))
        //    {
        //        return;
        //    }
        //    IDataConvertor convertor = Convertors.GetValue(Property.Type.Name);
        //    Value = convertor.Convert(value);
        //}

        //public string GetValue()
        //{
        //    if (Value == null)
        //    {
        //        return string.Empty;
        //    }
        //    IDataConvertor convertor = Convertors.GetValue(Property.Type.Name);
        //    return convertor.Convert(Value);
        //}

        //public void Default(User user)
        //{
        //    IDataConvertor convertor = Convertors.GetValue(Property.Type.Name);
        //    string text;
        //    object value;
        //    convertor.Default(user, out text, out value);
        //    Text = text;
        //    Value = value;
        //}

        //public T GetValue<T>()
        //{
        //    return (T)Value;
        //}

        //public object GetOriginalValue()
        //{
        //    return Value;
        //}

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsNull()
        {
            if (Value == null)
            {
                return true;
            }
            return false;
        }
    }
}
